export default class SceneUtil {
    constructor(viewer) {
        this.viewer = viewer;
        this.priManager =new Cesium.PrimitiveCollection();
        this.priManager.destroyPrimitives = false;
        viewer.scene.primitives.add(this.priManager);
    }

    loadTiles(option){
        this.tileset =new Cesium.Cesium3DTileset(
            option
        );
        this.tileset.readyPromise.then((tileset)=>{
            this.priManager.add(tileset);
            // if(!this.viewer.scene.primitives.contains(this.priManager)){
            //     this.viewer.scene.primitives.add(this.priManager);
            // }

            // this.viewer.zoomTo(tileset,new Cesium.HeadingPitchRange(
            //     0.0,
            //     -0.5,
            //     tileset.boundingSphere.radius * 2.0
            //     )
            // );
            if(option.height){
                this.setHeight(option.height);
            }
            if(option.style){
                this.tileset.style=option.style;
            }
        }).otherwise(function(error){console.log(error);});

    }

    loadModel({position={lon:0,lat:0,height:0},orientation={heading:0,pitch:0,roll:0},url="",scale=1}){
        let orgin =Cesium.Cartesian3.fromDegrees(position.lon,position.lat,position.height);
        let headingPitchRoll =new Cesium.HeadingPitchRoll(orientation.heading,orientation.pitch,orientation.roll);
        let modelMatrix =Cesium.Transforms.headingPitchRollToFixedFrame(orgin,headingPitchRoll);
        let model =this.priManager.add(
            Cesium.Model.fromGltf({
                url:url,
                modelMatrix:modelMatrix,
                scale:scale
            // minimumPixelSize:128,
            }));
        return model;
    }
    loadData({type='',url=''}){
        if(type=='kml'){
            this.viewer.dataSources.add(Cesium.KmlDataSource.load(url,{
                camera:this.viewer.scene.camera,
                canvas:this.viewer.scene.canvas
            }));
        }
    }
    setHeight(height){
        let tileset =this.tileset;
        let cartographic =Cesium.Cartographic.fromCartesian(tileset.boundingSphere.center);
        let surface =Cesium.Cartesian3.fromRadians(cartographic.longitude,cartographic.latitude,0.0);
        let offset =Cesium.Cartesian3.fromRadians(cartographic.longitude,cartographic.latitude,height);
        let translation =Cesium.Cartesian3.subtract(offset,surface,new Cesium.Cartesian3);
        tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
    }
    // 加载json形式的场景
    loadScene(sceneJson) {
        this.clear();
        // 加载模型
        let models = sceneJson.models;
        if (models) {
            console.log(sceneJson)
            for (let i = 0; i < models.length; i++) {
                let model = models[i];
                if (model.type == "3dtiles") {
                    this.loadTiles(model.options);
                } else if (model.type == "gltf") {
                    // this.loadModel(model.options);
                    this.loadModel(model.options);
                }
            }
        }
        let vectorFiles =sceneJson.vectorFiles;
        if(vectorFiles){
            vectorFiles.forEach((item)=>{
                this.loadData(item);
            })
        }
        // 相机飞行
        let camera = this.viewer.camera;
        let pos = sceneJson.position;
        let ort = sceneJson.orientation;
        camera.flyTo({
            destination: Cesium.Cartesian3.fromDegrees(pos.lon, pos.lat, pos.height),
            orientation: {
                heading: Cesium.Math.toRadians(ort.heading),
                pitch: Cesium.Math.toRadians(ort.pitch),
                roll: Cesium.Math.toRadians(ort.roll),
            }
        })
    }
    clear() {
        this.priManager.removeAll();
        this.viewer.dataSources.removeAll();
    }
}